package com.hanfengyi.consumer;

import com.alibaba.fastjson.JSONObject;
import com.hanfengyi.feign.FastDFSFignClient;
import com.hanfengyi.hrm.CreateFileUtils;
import com.hanfengyi.hrm.constants.RabbitMQConstants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.util.Map;

/**
 * User: Han
 * Date: 2020/2/27
 * Time: 22:44
 * Description:
 */
@Component
public class Consumer {
    @Autowired
    private FastDFSFignClient fastDFSFignClient;
    @RabbitListener(queues = {RabbitMQConstants.QUEUE_NAME_PAGE_STATIC})
    public void MQListener(String msg, Message message, Channel channel){
        //1. 从map中取出文件在fastdfs上的路径，以及站点路径
        System.out.println(msg);
        Map<String,String> map = JSONObject.parseObject(msg, Map.class);
        String htmlPath = map.get("htmlPath");
        String sitePath = map.get("sitePath");
    
        //2. 通过feign调用fastdfs,把html文件下载下来下载到对应站点路径
        byte[] templateFile = fastDFSFignClient.getTemplateFile(htmlPath);
        if(templateFile!=null || templateFile.length>0){
            try {
                FileCopyUtils.copy(templateFile, CreateFileUtils.createFile(sitePath));
                //3. 触发手动签收
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}